package zozopo.attendance;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import zozopo.attendance.operations.LeaveOS;
import zozopo.attendance.operations.LeaveTB;
import zozopo.attendance.operations.OperatorAttendanceOS;
import zozopo.attendance.operations.OperatorAttendanceTB;
import zozopo.background.operator.operations.DepartmentOS;
import zozopo.background.operator.operations.OperatorTB;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class AddLeaveAction extends ActionSupport implements Action,SessionAware{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	
	//传入参数
	private String date;
	
	private String begin_leave;
	private String end_leave;
	private String reason_leave;
	

	//传出参数
	private OperatorAttendanceTB operatorOfAttendance;
	private OperatorTB operator;
	
	
	
	private LeaveTB leave;
	
	private Map<Object, Object> session;
	public void setSession(Map arg0) {
		// TODO Auto-generated method stub
		session = arg0;
	}
	public String execute(){
		
		
		leave = new LeaveTB();
		LeaveOS leaveOS = new LeaveOS();
		DepartmentOS departmentOS = new DepartmentOS(leaveOS.getConn());
	
		leave.setDepartmentid_leave(operator.getDepartmentid_operator());
		leave.setDepartmentname_leave(departmentOS.getDepartmentWholename(operator.getDepartmentid_operator()));
		leave.setDepartmentlevel_leave(operator.getDepartmentlevel_operator());
		
		leave.setOperatorid_leave(operator.getId_operator());
		leave.setName_leave(operator.getName_operator());
		leave.setFirstname_leave(operator.getFirstname_operator());
		leave.setLastname_leave(operator.getLastname_operator());
		
		leave.setDate_leave(date);
		leave.setBegin_leave(begin_leave);
		leave.setEnd_leave(end_leave);
		
		leave.setReason_leave(reason_leave);
		
		
		if(leaveOS.apply(leave)){
			leave = leaveOS.getInfoByOperatorid(operator.getId_operator());
			leaveOS.releaseConnection();
			return SUCCESS;
		}else{
			leaveOS.releaseConnection();
			return ERROR;
		}
	
	}

	public void validate() {
		//权限处理
	
		operator = (OperatorTB)session.get("operatorInfo"); 
	
		if(operator == null){
			addFieldError("errormessage",getText("operator.timeoutornotlogin"));
			return ; 
		} 
		
		//日期比较
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date nowDate = new Date();
		Date dateToDate = new Date();
		
		try {
			dateToDate = sf.parse(date+" "+begin_leave);
			
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		if(dateToDate.getTime() < nowDate.getTime()){  //如果申请时间大于当前时间不可请假.
			addFieldError("errormessage","申请时间大于当前时间不可请假.");
			return ; 
		}

		
		//用户权限
		
		OperatorAttendanceOS operatorOfAttendanceOS = new OperatorAttendanceOS();
		
		LeaveOS leaveOS = new LeaveOS(operatorOfAttendanceOS.getConn());
		
		if(!operatorOfAttendanceOS.isExist(operator.getId_operator())){
			operatorOfAttendanceOS.releaseConnection();
			addFieldError("errormessage","用户未加入考勤系统无法请假!请联系管理人员.!!");
			return ; 
		}
		
		
		
		if(leaveOS.isExistDate(date, operator.getId_operator())){
			operatorOfAttendanceOS.releaseConnection();
			addFieldError("errormessage","操作员"+date+"已经请过假，无法同一天请1次以上假。");
			return ; 
		}
		
		
		operatorOfAttendance = operatorOfAttendanceOS.getInfoByOperatorid(operator.getId_operator());
		operatorOfAttendanceOS.releaseConnection();
	}
	public OperatorAttendanceTB getOperatorOfAttendance() {
		return operatorOfAttendance;
	}
	public void setOperatorOfAttendance(OperatorAttendanceTB operatorOfAttendance) {
		this.operatorOfAttendance = operatorOfAttendance;
	}
	public OperatorTB getOperator() {
		return operator;
	}
	public void setOperator(OperatorTB operator) {
		this.operator = operator;
	}

	public String getDate() {
		return date;
	}
	public void setDate(String date) {
		this.date = date;
	}
	public String getBegin_leave() {
		return begin_leave;
	}
	public void setBegin_leave(String begin_leave) {
		this.begin_leave = begin_leave;
	}
	public String getEnd_leave() {
		return end_leave;
	}
	public void setEnd_leave(String end_leave) {
		this.end_leave = end_leave;
	}
	public LeaveTB getLeave() {
		return leave;
	}
	public void setLeave(LeaveTB leave) {
		this.leave = leave;
	}
	public String getReason_leave() {
		return reason_leave;
	}
	public void setReason_leave(String reason_leave) {
		this.reason_leave = reason_leave;
	}
	
}
